package com.dede.baselib.state;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.dede.baselib.R;
import com.dede.state.State;
import com.dede.state.StateProxy;
import com.dede.state.Transport;
import com.dede.state.component.StateComponent;

/**
 * @author hsh
 * @date 2018/1/23 13:56.
 * @doc
 */
public class StateActivity extends AppCompatActivity {

    private StateProxy stateProxy;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_state);
        stateProxy = State.getDefault().register(this, new StateComponent.ReloadListener() {
            @Override
            public void reload(Class<? extends StateComponent> clazz) {
                stateProxy.showWithConverter(2);
            }
        }).customComponent(Loading.class, new Transport() {
            @Override
            public void alter(View view) {
                TextView textView = view.findViewById(R.id.tv_loading);
                textView.setText("loading");
            }
        });

        findViewById(R.id.bt_loading).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stateProxy.show(Loading.class);
            }
        });

        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.state_content, new StateFragment())
                .commit();
    }

    public void error(View view) {
        stateProxy.showWithConverter(-1);
    }

    public void empty(View view) {
        stateProxy.show(Empty.class);
    }
}
